package com.example.shixun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class SpendEmail {
    @Autowired(required = false)
    private JavaMailSender sender; // 引入Spring Mail依赖后，会自动装配到IOC容器。用来发送邮件
    @GetMapping ("/test")
    public String test1(@RequestParam("email")String email,String code){
        System.out.println("email:"+email);
        //code=code.trim();
        //String code = "6666";// 验证码
        if(email==null) {
            System.out.println("验证码发送成功！");
            return "发送失败，请输入邮箱!";
        }
        if(code==null||code.trim()==""){
            code = String.valueOf((long)((Math.random()*9+1)*100000));
            System.out.println(code);
            System.out.println("-----------------------");
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("【验证码】验证消息"); // 发送邮件的标题
        message.setText("验证码："+ code + "，切勿将验证码泄露给他人，本条验证码有效期3分钟。"); // 发送邮件的内容

        System.out.println(email);
        message.setTo(email); // 指定要接收邮件的用户邮箱账号
        message.setFrom("liu.ysh@foxmail.com"); // 发送邮件的邮箱账号，注意一定要和配置文件中的一致！
        sender.send(message); // 调用send方法发送邮件即可
        System.out.println("验证码发送成功！");
        return "放送成功";
    }


}
